package studentinformationmanagementsystem.servlet.admin;

import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.service.StudentService;
import studentinformationmanagementsystem.service.impl.StudenServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;
import studentinformationmanagementsystem.utils.GetMD5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.List;

@WebServlet("/admin/function/student.do")
public class StudentServlet extends BaseServlet {
    StudentService studentService=new StudenServiceImpl();
    //查询所有学生列表信息
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> students = studentService.queryAll();
        request.setAttribute("students",students);
        //通过请求转发，将请求对象转发到学生列表jsp上
        request.getRequestDispatcher("/admin/function/students.jsp").forward(request,response);
    }

    //根据学生id删除学生信息
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        Integer  id= Integer.valueOf(idStr);
        Student student = new Student();
        student.setStudid(id);
        int delete = studentService.delete(student);
//        response.sendRedirect("./student.do?action=queryAll");
        response.setContentType("text/html;charset=utf-8");
        if(delete>0){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    //添加学生信息
    public void insert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Student student = getParameters(request);
        int result = studentService.register(student);
        response.setContentType("text/html;charset=utf-8");
        if(result>0){
            //添加学生成功
            response.getWriter().print(1);
            HttpSession session = request.getSession();
            session.setAttribute("student",result);
        }
        else {
            response.getWriter().print(0);
        }
//        response.sendRedirect("./student.do?action=queryAll");
    }
    //根据学生id查询学生信息
    public void queryById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String idStr = request.getParameter("id");
        Integer id = Integer.valueOf(idStr);
        Student student = new Student();
        student.setStudid(id);
        Student studentinfo = studentService.queryById(student);
//        request.setAttribute("studentinfo",studentinfo);

//        request.getRequestDispatcher("/admin/function/edit-student.jsp").forward(request,response);
    //将查询到的学生信息保存到session域中
        request.getSession().setAttribute("studentinfo",studentinfo);
    }

    //根据学生的id修改学生信息
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
//            String idStr = request.getParameter("id");
//            Integer id = Integer.valueOf(idStr);
        Student studentinfo =(Student) request.getSession().getAttribute("studentinfo");
        Integer id= studentinfo.getStudid();
        Student student = getParameters(request);
        student.setStudid(id);
        int result=studentService.update(student);
        response.setContentType("text/html;charset=utf-8");
        if (result>0){
            response.getWriter().print(1);
        }

//        response.sendRedirect("./student.do?action=queryAll");
    }

    //判断学生学号是否重复
    public void isExists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        String studentid = request.getParameter("studentid");
        Student student = new Student();
        student.setStudentid(studentid);
        boolean existsUsername = studentService.isExistsUsername(student);
        if (existsUsername){
            response.getWriter().write("true");
        }

    }
        //获取学生信息
    public Student getParameters(HttpServletRequest request) throws UnsupportedEncodingException {
//        Integer id = Integer.valueOf(request.getParameter("id"));
        String studentid = request.getParameter("studentid");
        String name = request.getParameter("name");
        String idnumber = request.getParameter("idnumber");
        String sex = request.getParameter("sex");
        String password = request.getParameter("password");
        //加密操作后的密码
        String password2 = GetMD5.string2MD5(password);
        Date birthdate = Date.valueOf(request.getParameter("birthdate"));
//        Integer classandgradeid = Integer.valueOf(request.getParameter("classandgradeid"));
        String classandgradeid = request.getParameter("classandgradeid");
        String nation = request.getParameter("nation");
        Date admitionDate = Date.valueOf(request.getParameter("admitionDate"));
        String phone = request.getParameter("phone");
//        Integer departmentid = Integer.valueOf(request.getParameter("departmentid"));
        String departmentid = request.getParameter("departmentid");
        String politicsstatus = request.getParameter("politicsstatus");
        //获取从图片的唯一uuid值
        String photograph = request.getParameter("filename");
        String homeaddress = request.getParameter("homeaddress");
//        Integer major = Integer.valueOf(request.getParameter("major"));
        String major = request.getParameter("major");
        String mailbox = request.getParameter("mailbox");
        String nativeplace = request.getParameter("nativeplace");
        String emergencycontact = request.getParameter("emergencycontact");
        String emergencycontactphone = request.getParameter("emergencycontactphone");
        Student student = new Student(null, studentid, name, idnumber, sex, birthdate, nation, politicsstatus, photograph, phone, mailbox, classandgradeid, major, departmentid, homeaddress, emergencycontact, emergencycontactphone, nativeplace, password2,admitionDate,0);
        return student;
    }


}
